//vue
import Vue from 'vue';
//app.vue
import app from './app.vue'
import Vuex from 'vuex'

Vue.use(Vuex);
//导入mint-ui的Header
/*import { Header, Swipe ,SwipeItem ,Button } from 'mint-ui';
Vue.component(Header.name, Header);
Vue.component(Swipe.name, Swipe);
Vue.component(SwipeItem.name, SwipeItem);
Vue.component(Button.name, Button);*/
//导入mui
import './lib/dist/css/mui.min.css'
//导入路由
import VueRouter from 'vue-router';
Vue.use(VueRouter);
import router from './router.js'

//导入额外的字体图标
import './lib/dist/css/icons-extra.css'

// 2.1 导入 vue-resource
import VueResource from 'vue-resource'
// 2.2 安装 vue-resource
Vue.use(VueResource)
import moment from 'moment'

Vue.filter('dataFormat',function (dataStr,pattern='YYYY-MM-DD HH:mm:ss') {
    return moment(dataStr).format(pattern);
});
//导入懒加载模式
/*import { Lazyload } from 'mint-ui';
Vue.use(Lazyload);*/
import MintUI from 'mint-ui'
Vue.use(MintUI)
import 'mint-ui/lib/style.css'

//引入veu-preview
import VuePreview from 'vue-preview'
//使用vue-preview
Vue.use(VuePreview)
var car = JSON.parse(localStorage.getItem('car')||'[]');
const store = new Vuex.Store({
    state:{
        car:car,
    },
    mutations:{
        addToCart(state,obj){
            var flag = false;
            state.car.some((item)=>{
                if(item.id === obj.id){
                    item.count += parseInt(obj.count);
                    flag = true;
                    return true;
                }
            });
            if(!flag){
                state.car.push(obj);
            }
            localStorage.setItem('car',JSON.stringify(state.car));
        },
        updateGoodsInfo(state, goodsinfo) {
            // 修改购物车中商品的数量值
            // 分析：
            state.car.some(item => {
                if (item.id == goodsinfo.id) {
                    item.count = parseInt(goodsinfo.countNew);
                    return true
                }
            });
            // 当修改完商品的数量，把最新的购物车数据，保存到 本地存储中
            localStorage.setItem('car', JSON.stringify(state.car));
        },
        removeGood(state,id){
            state.car.some((item,i)=>{
                //注意这里使用===会出问题
                if(item.id == id){
                    state.car.splice(i,1);
                    return true;
                }
            });
            localStorage.setItem('car', JSON.stringify(state.car));
        },
        //改变switch的状态
        //保存到store里
        setSelected(state,obj){
            state.car.some(item => {
                if (item.id == obj.id) {
                    item.selected = obj.val;
                    return true
                }
            });
            // 当修改完商品的数量，把最新的购物车数据，保存到 本地存储中
            localStorage.setItem('car', JSON.stringify(state.car));
        },
    },
    getters:{
        getAllCount:function (state) {
            var c = 0;
            state.car.forEach((item)=>{
                c += item.count;
            });
            return c;
        },
        //得到购买的数量{id:count，。。。。}的形式
        getGoodCount:function (state) {
            var d = {};
            state.car.forEach((item)=>{
                d[item.id] = item.count;
            });
            return d;
        },
        getGoodsSelected:function (state) {
            var e = {};
            state.car.forEach((item)=>{
                e[item.id] = item.selected;
            });
            return e;
        },
        //更新件数和总价
        getTotal(state){
            var obj = {
                countTotal:0,
                priceTotal:0
            }
            state.car.forEach((item)=>{
                if(item.selected){
                    obj.countTotal += item.count;
                    obj.priceTotal += item.price * item.count;
                }
            });
            return obj;
        }
    },

})

console.log('ok');
var vm = new Vue({
    el:'#app',
    data:{
        msg:'123',
    },
    render:function (c) {
        return c(app);
    },
    router:router,
    store,
});
